package com.qf.util;

import com.qf.entity.SysUser;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.apache.shiro.web.util.WebUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 2019/9/25
 * Administrator
 * ssm0923
 * 面向对象面向君  不负代码不负卿
 */
public class MyFormFilter extends FormAuthenticationFilter {
    @Override
    protected boolean isLoginSubmission(ServletRequest request, ServletResponse response) {
        String rolename = request.getParameter("rolename");
        String username = request.getParameter("username");
        System.out.println("username="+username);
        System.out.println("rolename="+rolename);

        return super.isLoginSubmission(request, response);
    }

    @Override
    protected boolean onLoginSuccess(AuthenticationToken token, Subject subject,
                                     ServletRequest request, ServletResponse response) throws Exception {
       //1.得到认证通过的主体
        SysUser user = (SysUser) subject.getPrincipal();
        //2.存储到session中
        HttpServletRequest httpServletRequest = WebUtils.toHttp(request);
        HttpSession session = httpServletRequest.getSession();
        session.setAttribute("user1",user);
        return super.onLoginSuccess(token, subject, request, response);
    }
}
